import Cookie from 'js-cookie'
// 默认展示页面
export default {
  state: {
    themeColor: 'rgb(27,40,72)', // 主题色
    isCollapse: false,
    currentMenu: null,
    userMenu:[],// 用户所有菜单list
    menu: [],// 用户当前菜单tree
    tabsList: [
      // *******若要添加首页，只需要将此处注释打开，然后把login和应用切换部分的逻辑改为默认跳转至首页，而非菜单第一个****
      {
        path: '/',
        name: 'home',
        label: '欢迎页面',
        icon: 'home'
      }
    ]
  },
  mutations: {
    // 设置菜单
    setMenu(state, val) {
      state.menu = val
      sessionStorage.setItem('menu', JSON.stringify(val))
    },
    setUserMenu(state, val){
      state.userMenu = val
    },
    getTableList (state){
      if (!Cookie.get('tabsList')) {
        return
      }
       state.tabsList = JSON.parse(Cookie.get('tabsList'))
    },
    // 登出菜单
    clearMenu(state) {
      // 清空菜单
      state.menu = []
      state.tabsList = [
        {
          path: '/',
          name: 'home',
          label: '欢迎页面',
          icon: 'home'
        }
      ];
      sessionStorage.removeItem('menu');
      Cookie.remove('tabsList')
    },
    // 添加菜单
    addMenu(state, router) {
      if (!sessionStorage.getItem('menu')) {
        return
      }
      let menu = JSON.parse(sessionStorage.getItem('menu'))
      state.menu = menu
      let currentMenu = [
        {
          path: '/',
          component: resolve => require([`@/views/Main`],resolve),
          children: []
        }
      ]
      // 递归添加动态路由
      let mainRouters = []
       const ARouter = (menu)=>{
          menu.forEach(item => {
            if (item.children) {
              ARouter(item.children)
            } else {
              let queryUrl = item.url
              let path = ""
              let param = ""
              let str = queryUrl.indexOf('?')
              if(str != -1){
                path = queryUrl.substr(0,str)
                param = queryUrl.substr(str+1)
                item.path = item.path.split('?')[0] + "/" + param
              }
              else{
                path = queryUrl
                item.path = item.path.split('?')[0]
              }
              item.component = resolve => require([`@/views/${path}.vue`],resolve);
              mainRouters.push(item)
            }
          })
       }
       ARouter(menu) // 调用递归函数
      

      currentMenu[0].children= [...mainRouters];
      router.addRoutes(currentMenu)

    },
    selectMenu(state, val) {
        state.currentMenu = val
        let result = state.tabsList.findIndex(item => item.name === val.name)
        result === -1 ? state.tabsList.push(val) : ''
        Cookie.set('tabsList', JSON.stringify(state.tabsList))
    },
    toMessagePage(state) {
      let val = {
        path: 'message',
        name: 'message',
        label: '消息',
      }
      let result = state.tabsList.findIndex(item => item.name === val.name)
        result === -1 ? state.tabsList.push(val) : ''
        Cookie.set('tabsList', JSON.stringify(state.tabsList))
    },
    closeTab(state, val) {
      let res = state.tabsList.findIndex(item => item.name === val.name)
      state.tabsList.splice(res, 1)
      Cookie.set('tabsList', JSON.stringify(state.tabsList))
    },
    closeAllTab(state, val) {
      state.tabsList = [{
        path: '/',
        name: 'home',
        label: '欢迎页面',
        icon: 'home'
      }];
      Cookie.set('tabsList', JSON.stringify(state.tabsList))
    },
    collapseMenu(state) {
      state.isCollapse = !state.isCollapse
    }
  },
  actions: {}
}
